package com.gome.ss.core.queue;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class StanderdQueueCache<T> extends QueueCache<T> {

	private Map<Integer, Queue<?>> queueCachePriorityMap = new HashMap<Integer, Queue<?>>();

	public StanderdQueueCache() {
		for (int i = QueueCacheInterface.maxPriority; i >= QueueCacheInterface.minPriority; i -= QueueCacheInterface.priorityInterval) {
			this.queueCachePriorityMap.put(i, new LinkedList<T>());
		}
		this.queueCachePriorityMap.put(QueueCacheInterface.specialPriority, new LinkedList<T>());
	}
	
	@Override
	public synchronized void add(T obj, Integer priority) {
		// TODO Auto-generated method stub
		if (priority > QueueCacheInterface.maxPriority ||
				priority < QueueCacheInterface.minPriority) {
			if (priority != QueueCacheInterface.specialPriority)
				return;
		}
		@SuppressWarnings("unchecked")
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(priority);
		q.add(obj);
	}

	@Override
	public synchronized void remove(T obj) {
		// TODO Auto-generated method stub
		for (int i = QueueCacheInterface.maxPriority; i >= QueueCacheInterface.minPriority; i -= QueueCacheInterface.priorityInterval) {
			@SuppressWarnings("unchecked")
			Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(i);
			if (q.contains(obj)) {
				q.remove(obj);
				return;
			}
		}
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(QueueCacheInterface.specialPriority);
		if (q.contains(obj))
			q.remove(obj);
	}

	@Override
	public synchronized boolean contains(T obj) {
		// TODO Auto-generated method stub
		for (int i = QueueCacheInterface.maxPriority; i >= QueueCacheInterface.minPriority; i -= QueueCacheInterface.priorityInterval) {
			@SuppressWarnings("unchecked")
			Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(i);
			if (q.contains(obj)) {
				return true;
			}
		}
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(QueueCacheInterface.specialPriority);
		if (q.contains(obj)) {
			q.contains(obj);
			return true;
		}
		return false;
	}

	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		for (int i = QueueCacheInterface.maxPriority; i >= QueueCacheInterface.minPriority; i -= QueueCacheInterface.priorityInterval) {
			@SuppressWarnings("unchecked")
			Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(i);
			if (!q.isEmpty()) {
				return false;
			}
		}
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(QueueCacheInterface.specialPriority);
		if (!q.isEmpty())
			return false;
		return true;
	}

	@Override
	public T element() {
		// TODO Auto-generated method stub
		for (int i = QueueCacheInterface.maxPriority; i >= QueueCacheInterface.minPriority; i -= QueueCacheInterface.priorityInterval) {
			@SuppressWarnings("unchecked")
			Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(i);
			if (!q.isEmpty()) {
				return q.element();
			}
		}
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(QueueCacheInterface.specialPriority);
		if (!q.isEmpty())
			return q.element();
		return null;
	}

	@Override
	public synchronized boolean offer(T obj, Integer priority) {
		// TODO Auto-generated method stub
		if (priority > QueueCacheInterface.maxPriority ||
				priority < QueueCacheInterface.minPriority) {
			if (priority != QueueCacheInterface.specialPriority)
				return false;
		}
		@SuppressWarnings("unchecked")
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(priority);
		return q.offer(obj);
	}

	@Override
	public synchronized T poll() {
		// TODO Auto-generated method stub
		for (int i = QueueCacheInterface.maxPriority; i >= QueueCacheInterface.minPriority; i -= QueueCacheInterface.priorityInterval) {
			@SuppressWarnings("unchecked")
			Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(i);
			if (!q.isEmpty()) {
				return q.poll();
			}
		}
		return null;
	}
	
	public synchronized T pollFromSpecial() {
		// TODO Auto-generated method stub
		@SuppressWarnings("unchecked")
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(QueueCacheInterface.specialPriority);
		if (!q.isEmpty())
			return q.poll();
		return null;
	}

	@Override
	public synchronized T peek() {
		// TODO Auto-generated method stub
		for (int i = QueueCacheInterface.maxPriority; i >= QueueCacheInterface.minPriority; i -= QueueCacheInterface.priorityInterval) {
			@SuppressWarnings("unchecked")
			Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(i);
			if (!q.isEmpty()) {
				return q.peek();
			}
		}
		Queue<T> q = (Queue<T>) this.queueCachePriorityMap.get(QueueCacheInterface.specialPriority);
		if (!q.isEmpty())
			return q.peek();
		return null;
	}

	@Override
	public synchronized void put(T obj, Integer priority) {
		// TODO Auto-generated method stub
	}

	@Override
	public synchronized T take() {
		// TODO Auto-generated method stub
		return null;
	}

}
